<script setup>
import { ref, reactive } from 'vue';
import { getSmsCode,reset } from '@/api/login';
import { useRouter } from "vue-router";
const router = useRouter()
const countdown = ref(0);
let form = reactive({
	password: '',
	phone: '',
	smscode: '',
})
const startCountdown = () => {
  countdown.value = 60; // 假设倒计时60秒
  const intervalId = setInterval(() => {
    if (countdown.value > 0) {
      countdown.value--;
    } else {
      clearInterval(intervalId);
    }
  }, 1000);
};
const validatePhoneNumber = (phoneNumber) => {
	const phoneRegex = /^1[34578]\d{9}$/;
	if (phoneRegex.test(phoneNumber)) {
		return true;
	} else {
		return false;
	}
}
const getSmsCodeHandler = () => {
	if(!validatePhoneNumber(form.phone)) {
		ElMessage.warning('请输入正确的手机号！');
		return false;
	}
	let params = {
		data:{phone:form.phone}
	}
	getSmsCode(params).then(res=>{
		if(res.code === 0){
			startCountdown();
			ElMessage.success('验证码已发送！');
			ElMessage.success('验证码是'+res.data.smscode);
		}
	})
}
const submit = () => {
	if(!validatePhoneNumber(form.phone)) {
		ElMessage.warning('请输入正确的手机号！');
		return false;
	}
	if(!form.smscode){
		ElMessage.warning('请输入验证码！');
		return false;
	}
	if(!form.password){
		ElMessage.warning('请输入密码！');
		return false;
	}
	let params = {
		data:{
			phone:form.phone,
			password:form.password,
			smscode:form.smscode.toString(),
			account:form.phone,
		}
	}
	reset(params).then(res=>{
		console.log(res)
		if(res.code === 0){
			ElMessage.success('重置成功！');
			router.push('/login');
		}else{
			ElMessage.error(res.msg);
		}
	})
}
</script>
<template>
	<div class="container">
		<div class="content">
			<div class="title">找回密码</div>
			<div class="form-content">
				<div class="form-item">
					<label for="id1" class="accout"></label>
					<input id="id1" type="text" v-model="form.phone" placeholder="请输入手机号" />
				</div>
				<div class="form-item">
					<label for="id2" class="code"></label>
					<input id="id2" type="number" v-model="form.smscode" placeholder="请输入验证码" />
					<button type="button" class="get-code" :disabled="countdown > 0" @click="getSmsCodeHandler">
						{{ countdown > 0 ? `${countdown}秒后重新获取` : '获取验证码' }}
					</button>
				</div>
				<div class="form-item">
					<label for="id3" class="password"></label>
					<input id="id3" type="password" v-model="form.password" placeholder="请输入密码" />
				</div>
				<button type="button" class="submit" @click="submit">提交</button>
				<div class="tip">
					<router-link to="/login">
						<span class="goLogin">返回上一级页面</span>
					</router-link>
				</div>
			</div>
		</div>
	</div>
</template>
<style lang="scss" scoped>
.container{
	width: 100%;
	background-image: url(../../assets/images/registerBg.png);
	background-position: 0 0;
	background-repeat: no-repeat;
	background-size: cover;
	display: flex;
	align-items: center;
	justify-content: center;
}
.content{
	width: 736px;
	height: 688px;
	background: #FFFFFF;
	border-radius: 30px;
	overflow: hidden;
}
.form-content{
	margin-top: 60px;
}
.form-item{
	width: 480px;
	height: 60px;
	background: rgba(0,0,0,0.05);
	border-radius: 5px;
	margin: 0 auto 40px auto;
	display: flex;
	align-items: center;
	padding: 0 22px;
	box-sizing: border-box;
	position: relative;
}
.form-item input{
    height: 25px;
	width: 400px;
	margin-left: 22px;
	border: none;
	background-color: transparent;
	padding-left: 20px;
	border-left: 1px solid rgba(0,0,0,0.3);
	box-sizing: border-box;
	outline: none;
}
.get-code{
	position: absolute;
	right: 20px;
	top: 18px;
}
.accout{
	display: block;
	width: 17px;
	height: 25px;
	background: url(../../assets/icons/accout.png) no-repeat center center/100%;
}
.code{
	display: block;
	width: 22px;
	height: 25px;
	background: url(../../assets/icons/code.png) no-repeat center center/100%;
}
.password{
	display: block;
	width: 20px;
	height: 25px;
	background: url(../../assets/icons/password.png) no-repeat center center/100%;
}
.agree{
	width: 410px;
	height: 20px;
	margin: 0 auto;
	cursor: pointer;
	font-size: 18px;
	color: #333333;
}
.title{
	font-family: MiSansVF_Medium;
	font-size: 28px;
	color: #45A1FF;
	line-height: 37px;
	text-align: center;
	margin-top: 80px;
}
.submit{
	display: block;
	width: 410px;
	height: 60px;
	line-height: 60px;
	background: #45A1FF;
	border-radius: 5px;
	font-family: MiSansVF_Medium;
	font-size: 22px;
	color: #FFFFFF;
	letter-spacing: 20px;
	text-align: center;
	border: none;
	margin: 30px auto 10px auto;
}
.tip{
	width: 410px;
	margin: 0 auto;
	display: flex;
	justify-content: center;
	a{
		text-decoration: none;
	}
	.goLogin{
		color: #45A1FF;
	}
}
.forget .forget-text{
	font-family: MiSansVF;
	font-size: 18px;
	color: #333333;
	line-height: 24px;
}
.get-code{
	font-family: MiSansVF;
	font-size: 18px;
	color: #45A1FF;
	border: none;
	cursor: pointer;
}
:deep(.el-checkbox__inner) { 
      width: 17px;
      height: 17px;
      border-color: #979797;
      border-radius: 2px;
}
:deep(.el-checkbox__inner::after){
	top: 2px;
	left: 5px;
}	
:deep(.el-checkbox){
	height: 20px;
}
</style>
